package com.alibaba.algo.controller.app;

import com.alibaba.algo.annotation.OperateLogAnnotation;
import com.alibaba.algo.apiimpl.app.FunctionInfoServiceImpl;
import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.usercenter.GlobalResultStatus;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.CreateFunctionRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.DeleteFunctionRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.app.UpdateFunctionRequestVO;
import com.alibaba.algo.enums.OperateTypeEnum;
import com.alibaba.algo.enums.ResourceTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 类FunctionInfoController.java的实现描述：功能 Controller
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@RestController
@RequestMapping(value = "/usercenter-admin-app/admin/functionInfo")
public class FunctionInfoController {

    @Autowired
    private FunctionInfoServiceImpl functionInfoService;

    @PostMapping("/insert")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.FUNCTION_INFO,operateType = OperateTypeEnum.INSERT)
    public BaseResult insert(@RequestBody CreateFunctionRequestVO vo) {
        if (StringUtils.isEmpty(vo.getFunctionCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_FUNCTION_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_FUNCTION_CODE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getFunctionName())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_FUNCTION_NAME_ERROR.getCode(), GlobalResultStatus.PARAM_FUNCTION_NAME_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getFunctionState())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_FUNCTION_STATE_ERROR.getCode(), GlobalResultStatus.PARAM_FUNCTION_STATE_ERROR.getMsg());
        }
        functionInfoService.insert(vo);
        return BaseResult.buildSuccess();
    }

    @PostMapping("/update")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.FUNCTION_INFO,operateType = OperateTypeEnum.UPDATE)
    public BaseResult update(@RequestBody UpdateFunctionRequestVO vo) {
        if (StringUtils.isEmpty(vo.getFunctionCode())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_FUNCTION_CODE_ERROR.getCode(), GlobalResultStatus.PARAM_FUNCTION_CODE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getFunctionName())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_FUNCTION_NAME_ERROR.getCode(), GlobalResultStatus.PARAM_FUNCTION_NAME_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getFunctionState())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_FUNCTION_STATE_ERROR.getCode(), GlobalResultStatus.PARAM_FUNCTION_STATE_ERROR.getMsg());
        }
        if (StringUtils.isEmpty(vo.getId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_FUNCTION_ID_ERROR.getCode(), GlobalResultStatus.PARAM_FUNCTION_ID_ERROR.getMsg());
        }
        functionInfoService.update(vo);
        return BaseResult.buildSuccess();
    }

    @PostMapping("/delete")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.FUNCTION_INFO,operateType = OperateTypeEnum.DELETE)
    public BaseResult delete(@RequestBody DeleteFunctionRequestVO vo) {
        if (StringUtils.isEmpty(vo.getFunctionId())) {
            return BaseResult.buildError(GlobalResultStatus.PARAM_FUNCTION_ID_ERROR.getCode(), GlobalResultStatus.PARAM_FUNCTION_ID_ERROR.getMsg());
        }
        functionInfoService.delete(vo);
        return BaseResult.buildSuccess();
    }

    @PostMapping("/selectList")
    public BaseResult selectList() {
        return functionInfoService.selectList();
    }

}
